﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="ExceptionGame.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011
//       Roland Rosenkranz (Glatzemann@email.de)
//
//    Parts of this code are based on code by Nick Gravelyn (http://blog.nickgravelyn.com/2009/07/a-more-robust-exception-system/)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 3201                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-01-10 14:48:36 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Game
{
    #region Using Statements
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using starLiGHT.Resources;

    #endregion

    public class ExceptionGame : Game
    {
        private readonly Exception exception;
        private bool shownMessage;
        private bool displayException;

        private SpriteBatch batch;
        private SpriteFont font;

        public ExceptionGame(Exception e)
        {
            new GraphicsDeviceManager(this)
            {
                PreferredBackBufferWidth = 1280,
                PreferredBackBufferHeight = 720
            };
            this.exception = e;

#if !WINDOWS_PHONE
            Components.Add(new GamerServicesComponent(this));
#endif
            Content.Dispose();
            Content = new ResourceContentManager(Services, ContentResources.ResourceManager);
        }

        protected override void LoadContent()
        {
            this.batch = new SpriteBatch(GraphicsDevice);
            this.font = Content.Load<SpriteFont>("Debug");
        }

        protected override void Update(GameTime gameTime)
        {
            if (!this.shownMessage)
            {
                try
                {
                    if (!Guide.IsVisible)
                    {
#if !ZUNE
                        Guide.BeginShowMessageBox(
                           PlayerIndex.One,
                           LocalizationResources.ErrorTitle,
                           LocalizationResources.ErrorMessage,
                           new string[] { LocalizationResources.ErrorButtonExit, LocalizationResources.ErrorButtonDetails },
                           0,
                           MessageBoxIcon.Error,
                           result =>
                           {
                               int? choice = Guide.EndShowMessageBox(result);

                               if (choice.HasValue && choice.Value == 1)
                               {
                                   displayException = true;
                               }
                               else
                               {
                                   Exit();
                               }
                           },
                           null);
                        this.shownMessage = true;
#endif
                    }
                }
                catch 
                { 
                }
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            if (this.displayException)
            {
                Vector2 p = new Vector2(
                   GraphicsDevice.Viewport.TitleSafeArea.X,
                   GraphicsDevice.Viewport.TitleSafeArea.Y);
                this.batch.Begin();
                this.batch.DrawString(this.font, this.exception.ToString(), p, Color.White);
                this.batch.End();
            }

            base.Draw(gameTime);
        }
    }
}
